$(function () {

    $('.btn-primary').click(function () {
        let userid = sessionStorage.getItem('userid')
        let bankName = $('#bankName option:selected').val()
        let branchName = $('#branchName').val()
        let cardNumber = $('#cardNumber').val()
        //console.log(userid,bankName,branchName,cardNumber);
        //console.log(bankName);
        switch (bankName) {
            case '1':
                bankName = '中国银行'
                break;
            case '2':
                bankName = '工商银行'
                break;
            case '3':
                bankName = '农业银行'
                break;
            case '4':
                bankName = '招商银行'
                break;
            default:
                break;
        }
        $.ajax({
            url: 'http://139.9.177.51:3333/p2p/bindCard',
            type: 'post',
            data: { userid, bankName, branchName, cardNumber },
            dataType: 'json',
            success: function (res) {
                if (res.code == 200) {
                    alert('绑定成功')
                    getCard()
                } else {
                    alert('绑定失败')
                }
            }
        })
    })
    getCard()
    function getCard() {
        let userid = sessionStorage.getItem('userid')
        $.ajax({
            url: 'http://139.9.177.51:3333/p2p/cardList',
            type: 'get',
            data: { userid },
            dataType: 'json',
            success: function (data) {
                if (data.code == 200) {
                    console.log(data.data);
                    //sessionStorage.setItem('id',data.data.id)
                    let leng = data.data.length;
                    let res = data.data;
                    let htmlS = '';
                    for (let i = 0; i < leng; i++) {
                        sessionStorage.setItem('id',`${res[i].id}`)
                        htmlS += `
                        <li class="col-md-4">
                        <p>
                            银行卡名称：
                            <span>${res[i].cardNumber}</span>
                        </p>
                        <p>
                            卡号：
                            <span>${res[i].cardNumber}</span>
                        </p>
                    </li>
                        `

                    }
                    $('#cardList').html(htmlS)
                } else {
                    alert('获取银行卡失败')
                }
            }
        })
    }
})